<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Cookie
 *
 * @author kubin
 */
class Cookie {

    //put your code here
    private $expire;

    public function __construct() {
        //echo 'COOKIE : ';
        //print_r($_COOKIE);
        if (!isset($_COOKIE["uid"], $_COOKIE["username"], $_COOKIE["status"], $_COOKIE["logged"])) {

            $this->setupCookie(0, " ", USER_NOTYET_LOGIN, FALSE);
           // $this->write("construct", "not yet set cookie");
        } else {
            $this->setupCookie($_COOKIE["uid"], $_COOKIE["username"], $_COOKIE["status"], $_COOKIE["logged"]);
           // $this->writeCookie("contructer");
        }
    }

    public function write($block, $string) {
        $testFile = fopen("testCookie.txt", 'a');
        fwrite($testFile, "Cookie.php\n");
        fwrite($testFile, date("h:m:s" . "\n"));
        fwrite($testFile, $block . ":\n");
        fwrite($testFile, "> " . $string . "\n\n");
        fclose($testFile);
    }

    public function writeCookie($block) {
        $testFile = fopen("testCookie.txt", 'a');
        fwrite($testFile, "Cookie.php\n");
        fwrite($testFile, date("h:m:s" . "\n"));
        fwrite($testFile, $block . " - cookie:\n");
        if (isset($_COOKIE["uid"], $_COOKIE["username"], $_COOKIE["status"], $_COOKIE["logged"])) {
            fwrite($testFile, "> uid: \t" . $_COOKIE["uid"] . "\n");
            fwrite($testFile, "> username: \t" . $_COOKIE["username"] . "\n");
            fwrite($testFile, "> status: \t" . $_COOKIE["status"] . "\n");
            fwrite($testFile, "> logged: \t" . $_COOKIE["logged"] . "\n");
        }
        else{
           fwrite($testFile, "> not yet setup cookie\n"); 
        }
        fwrite($testFile, "\n");
        
        fclose($testFile);
    }

    public function setupCookie($uid, $username, $status, $logged) {
        if ($status == USER_STANDARD || USER_BANNED) {
            $this->expire = time() + 86400 * 7;
        } else {
            $this->expire = time() + 3600;
        }


        //echo $uid . ", " . $username . ", " . $status . ", " . $logged;
//bool setcookie ( string $name [, string $value [, int $expire = 0 [, string $path [, string $domain [, bool $secure = false [, bool $httponly = false ]]]]]] )
        setcookie("uid", $uid, $this->expire, "/");
        setcookie("username", $username, $this->expire, "/");
        setcookie("status", $status, $this->expire, "/");
        setcookie("logged", $logged, $this->expire,"/");

        //$this->writeCookie("setupCookie");
        //echo '<br/>run here<br/>';
    }

    public function isLogged() {
        if (isset($_COOKIE["logged"])) {
         //   $this->write("is logge", $_COOKIE["logged"]);
            return $_COOKIE["logged"];
        } else {
          //  $this->write("is logge", 0);
            return false;
        }
    }

    public function getUid() {
		if (isset($_COOKIE["uid"])) {
			return $_COOKIE["uid"];
		}
		else return null;
    }

    public function getUsername() {
        if (isset($_COOKIE["username"])) {
			return $_COOKIE["username"];
		}
		else return null;
    }

    public function getStatus() {
        if (isset($_COOKIE["status"])) {
			return $_COOKIE["status"];
		}
		else return null;
    }

    public function close() {
				
        //setcookie("uid", NULL, time() - 1);
        //setcookie("username", null, time() - 1);
        //setcookie("logged", NULL, time() - 1);
        //setcookie("status", null, time() - 1);
        $this->setupCookie(0, " ", USER_NOTYET_LOGIN, FALSE);
       // $this->writeCookie("closse123:==========");
    }

}

?>
